using System;
using System.Diagnostics.Metrics;
using System.IO;
using System.Net.NetworkInformation;

using System;
using System.Collections.Generic;

// 定义组件接口
public interface IComponent
{
    void Add(IComponent component);
    void Remove(IComponent component);
    void Display(int depth);
}

// 定义叶子类
public class Leaf : IComponent
{
    private string _name;

    public Leaf(string name)
    {
        _name = name;
    }

    public void Add(IComponent component)
    {
        throw new InvalidOperationException("Cannot add to a leaf.");
    }

    public void Remove(IComponent component)
    {
        throw new InvalidOperationException("Cannot remove from a leaf.");
    }

    public void Display(int depth)
    {
        Console.WriteLine(new string('-', depth) + _name);
    }
}

// 定义组合类
public class Composite : IComponent
{
    private string _name;
    private List<IComponent> _children = new List<IComponent>();

    public Composite(string name)
    {
        _name = name;
    }

    public void Add(IComponent component)
    {
        _children.Add(component);
    }

    public void Remove(IComponent component)
    {
        _children.Remove(component);
    }

    public void Display(int depth)
    {
        Console.WriteLine(new string('-', depth) + _name);
        foreach (IComponent component in _children)
        {
            component.Display(depth + 2);
        }
    }
}

// 客户端代码
public class Client
{
    public static void Main()
    {
        IComponent root = new Composite("Root");
        IComponent child1 = new Composite("Child1");
        IComponent leaf1 = new Leaf("Leaf1");
        IComponent leaf2 = new Leaf("Leaf2");

        root.Add(child1);
        child1.Add(leaf1);
        child1.Add(leaf2);

        root.Display(1);
    }
}

//输出结果
//-Root
//-- - Child1
//---- - Leaf1
//---- - Leaf2